package io.renren.modules.sys.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.LowershelvesitemService;
import io.renren.modules.sys.service.ProductshelvesstcokService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.LowershelvesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-26 14:49:17
 */
@RestController
@RequestMapping("sys/lowershelves")
public class LowershelvesController {
    @Autowired
    private LowershelvesService lowershelvesService;

    @Autowired
    private LowershelvesitemService lowershelvesitemService;

    @Autowired
    private ProductshelvesstcokService productshelvesstcokService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:lowershelves:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = lowershelvesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:lowershelves:info")
    public R info(@PathVariable("id") Integer id){
        LowershelvesEntity lowershelves = lowershelvesService.getById(id);

        return R.ok().put("lowershelves", lowershelves);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoById/{id}")
    @RequiresPermissions("sys:lowershelves:infoById")
    public R infoById(@PathVariable("id") Integer id){
        LowershelvesVOEntity lowershelves = lowershelvesService.getInfoById(id);

        return R.ok().put("lowershelves", lowershelves);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:lowershelves:save")
    public R save(@RequestBody  String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        JSONObject json = JSONObject.fromObject(str);
        JSONArray arrayProduct = JSONArray.fromObject(json.get("products"));

        int count = 0;
        int price = 0;

        for(int i=0;i<arrayProduct.size();i++){
            count +=arrayProduct.getJSONObject(i).getInt("count");
            price +=count*arrayProduct.getJSONObject(i).getInt("costprice");
        }

        LowershelvesEntity lowershelvesEntity = new LowershelvesEntity();
        lowershelvesEntity.setLowernumber("XJ"+sdf.format(new Date()));
        lowershelvesEntity.setShelvesid(json.getInt("shelvesId"));
        lowershelvesEntity.setReason(json.getInt("reason"));
        lowershelvesEntity.setCreatetime(new Date());
        lowershelvesEntity.setCreateuser(Integer.parseInt(user.getUserId().toString()));
        lowershelvesEntity.setType(arrayProduct.size());
        lowershelvesEntity.setNum(count);
        lowershelvesEntity.setCost(new BigDecimal(price));

        lowershelvesService.save(lowershelvesEntity);

        for(int i=0;i<arrayProduct.size();i++){
            LowershelvesitemEntity lowershelvesitemEntity = new LowershelvesitemEntity();
            lowershelvesitemEntity.setProductid(arrayProduct.getJSONObject(i).getInt("id"));
            lowershelvesitemEntity.setLowercount(arrayProduct.getJSONObject(i).getInt("count"));
            lowershelvesitemEntity.setLowershelvesid(lowershelvesEntity.getId());
            lowershelvesitemService.save(lowershelvesitemEntity);

            //减去该货架库存中对应的下架数量
            ProductshelvesstcokEntity productshelvesstcokEntity = productshelvesstcokService.getByProductIdAndShelvesId(arrayProduct.getJSONObject(i).getInt("id"),json.getInt("shelvesId"));

            productshelvesstcokEntity.setStock(productshelvesstcokEntity.getStock()-arrayProduct.getJSONObject(i).getInt("count"));
            productshelvesstcokService.updateById(productshelvesstcokEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:lowershelves:update")
    public R update(@RequestBody LowershelvesEntity lowershelves){
        ValidatorUtils.validateEntity(lowershelves);
        lowershelvesService.updateById(lowershelves);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:lowershelves:delete")
    public R delete(@RequestBody Integer[] ids){
        lowershelvesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
